﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net;

namespace ThreadBaiduSearch
{
    public partial class Form1 : Form
    {
        string Keyword = "";
        private delegate void Delegate();
        public Form1()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Keyword = textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (Keyword == "")
                {
                    richTextBox1.Text = "关键词不能为空!";
                    richTextBox2.Text = "关键词不能为空！";
                }
                else
                {
                    Thread search1 = new Thread(() => { search_result1("https://www.baidu.com/baidu?wd=" + Keyword); });
                    Thread search2 = new Thread(() => { search_result2("https://www.baidu.com/baidu?wd=" + Keyword); });
                    search1.Start();
                    search2.Start();
                }
            }
            catch(Exception error)
            {
                richTextBox1.Text = Convert.ToString(error);
                richTextBox2.Text = Convert.ToString(error);
            }
        }
        private void search_result1(string url)
        {
            WebClient webClient = new WebClient();
            byte[] getdata = webClient.DownloadData(url);
            string response = Encoding.UTF8.GetString(getdata);
            this.BeginInvoke(new Delegate(() => { richTextBox1.Text = response.Substring(0,500); }));
        }
        private void search_result2(string url)
        {
            WebClient webClient = new WebClient();
            byte[] getdata = webClient.DownloadData(url);
            string response = Encoding.UTF8.GetString(getdata);
            this.BeginInvoke(new Delegate(() => { richTextBox2.Text = response.Substring(500, 500); }));
        }

        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Keyword == "")
            {
                richTextBox1.Text = "关键词不能为空!";
                richTextBox2.Text = "关键词不能为空！";
            }
            else
            {
                WebClient webClient = new WebClient();
                webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadCompleted);
                webClient.DownloadDataAsync(new Uri("https://www.baidu.com/baidu?wd=" + Keyword));
                webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadCompleted2);
                label5.Text = "搜索中……";
                label6.Text = "搜索中……";
            }
        }
        private void DownloadCompleted(Object sender, DownloadDataCompletedEventArgs e)
        {
            try
            {
                if ((e.Error == null) && (!e.Cancelled))
                {
                    byte[] bytes = e.Result;
                    string response = Encoding.UTF8.GetString(bytes);
                    this.BeginInvoke(new Delegate(() => { richTextBox1.Text = response.Substring(0, 100); }));
                    label5.Text = "搜索完成！";
                }
                // do something with the bytes

            }
            catch(Exception error)
            {
                richTextBox1.Text = Convert.ToString(error);
                richTextBox2.Text = Convert.ToString(error);
            }
        }
        private void DownloadCompleted2(Object sender, DownloadDataCompletedEventArgs e)
        {
            try
            {
                if ((e.Error == null) && (!e.Cancelled))
                {
                    byte[] bytes = e.Result;
                    string response = Encoding.UTF8.GetString(bytes);
                    this.BeginInvoke(new Delegate(() => { richTextBox2.Text = response.Substring(100, 100); }));
                    label6.Text = "搜索完成！";
                }
            }
            catch (Exception error)
            {
                richTextBox1.Text = Convert.ToString(error);
                richTextBox2.Text = Convert.ToString(error);
            }
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
